<template>
  <div>
    <el-form :model="mallConfig" :rules="rules" ref="postForm" label-width="300px" label-position="right">
      <el-form-item label="第一级抽成比例(单位%)：" prop="distL1Ratio">
        <el-input v-model="mallConfig.distL1Ratio" placeholder="请输入内容" style="width: 400px"></el-input>
      </el-form-item>
      <el-form-item label="第二级抽成比例(单位%)：" prop="distL2Ratio">
        <el-input v-model="mallConfig.distL2Ratio" placeholder="请输入内容" style="width: 400px"></el-input>
      </el-form-item>
      <el-form-item label="第三级抽成比例(单位%)：" prop="distL3Ratio">
        <el-input v-model="mallConfig.distL3Ratio" placeholder="请输入内容" style="width: 400px"></el-input>
      </el-form-item>
      <el-form-item label="代理商抽成比例(单位%)：" prop="distAgentRatio" v-if="this.agentLevel > 0">
        <el-input v-model="mallConfig.distAgentRatio" placeholder="请输入内容" style="width: 400px"></el-input>
      </el-form-item>
      <el-form-item label="合伙人抽成比例(单位%)：" prop="distPartnerRatio" v-if="this.agentLevel > 1">
        <el-input v-model="mallConfig.distPartnerRatio" placeholder="请输入内容" style="width: 400px"></el-input>
      </el-form-item>
      <el-form-item label="分公司抽成比例(单位%)：" prop="distBranchRatio" v-if="this.agentLevel > 1">
        <el-input v-model="mallConfig.distBranchRatio" placeholder="请输入内容" style="width: 400px"></el-input>
      </el-form-item>
      <el-form-item label="是否支持发票：">
        <el-radio style='width:100px;' v-model="mallConfig.isInvoce" :label="1">是</el-radio>
        <el-radio style='width:100px;' v-model="mallConfig.isInvoce" :label="-1">否</el-radio>
      </el-form-item>
      <p v-if="permission == 3" style="text-align: center;">
        <el-button type="primary" icon="el-icon-check" @click="save('commission')">保存</el-button>
      </p>
    </el-form>
  </div>
</template>
<script>
  import { validatePercent } from '@/utils/vm'
  import { getAgentLevels } from '@/utils/permission'
  export default {
    props: ['mallConfig', 'permission'],
    data() {
      return {
        agentLevel: 0,
        rules: {
          distL1Ratio: [
            { type: 'number', validator: validatePercent, trigger: 'blur' }
          ],
          distL2Ratio: [
            { type: 'number', validator: validatePercent, trigger: 'blur' }
          ],
          distL3Ratio: [
            { type: 'number', validator: validatePercent, trigger: 'blur' }
          ],
          distAgentRatio: [
            { type: 'number', validator: validatePercent, trigger: 'blur' }
          ],
          distPartnerRatio: [
            { type: 'number', validator: validatePercent, trigger: 'blur' }
          ],
          distBranchRatio: [
            { type: 'number', validator: validatePercent, trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.agentLevel = getAgentLevels()
      console.log('sdsadafa', this.agentLevel)
    },
    methods: {
      save(msg) {
        if (this.mallConfig.distL1Ratio < 0 || this.mallConfig.distL2Ratio < 0 || this.mallConfig.distL3Ratio < 0 || this.mallConfig.distAgentRatio < 0 || this.mallConfig.distPartnerRatio < 0 || this.mallConfig.distBranchRatio < 0) {
          this.$message({
            message: '抽成比例不能为负数',
            type: 'error'
          })
          return
        }
        if (this.mallConfig.distL1Ratio > 100 || this.mallConfig.distL2Ratio > 100 || this.mallConfig.distL3Ratio > 100 || this.mallConfig.distAgentRatio > 100 || this.mallConfig.distPartnerRatio > 100 || this.mallConfig.distBranchRatio > 100) {
          this.$message({
            message: '抽成比例不能大于100',
            type: 'error'
          })
          return
        }
        if (this.mallConfig.distL1Ratio == 0) {
          if (this.mallConfig.distL2Ratio != 0 || this.mallConfig.distL3Ratio != 0) {
            this.$message({
              message: '第一级抽成比例为0，第二三级也只能为0',
              type: 'error'
            })
            return
          }
        }
        if (this.mallConfig.distL2Ratio == 0) {
          if (this.mallConfig.distL3Ratio != 0) {
            this.$message({
              message: '第二级抽成比例为0，第三级也只能为0',
              type: 'error'
            })
            return
          }
        }
        if (Number(this.mallConfig.distL1Ratio) + Number(this.mallConfig.distL2Ratio) + Number(this.mallConfig.distL3Ratio) + Number(this.mallConfig.distAgentRatio) + Number(this.mallConfig.distPartnerRatio) + Number(this.mallConfig.distBranchRatio) > 100) {
          this.$message({
            message: '一二三跟代理商、合伙人、分公司抽成比例总和不能大于100',
            type: 'error',
            align: 'center'
          })
          return
        }
        this.$refs.postForm.validate(valid => {
          if (valid) {
            this.$emit('save', msg)
          } else {
            this.$message({
              message: '内容输入有误，请重新输入',
              type: 'error'
            })
          }
        })
      }

    }
  }
</script>
<style scoped>
  .boder-bottom {
    border-bottom: 1px solid #eee;
    padding: 10px 0 10px 50px;
  }

  .boder-bottom p {
    line-height: 55px;
  }

  .boder-bottom label {
    display: inline-block;
    width: 300px;
    margin-right: 30px;
    text-align: right;
  }
</style>
